

Page({
    data: {
		list:[],
		
		defaultList:[],
		code:"",
		date:"",
		win:"",

		my:[
			[{num:4},{num:1},{num:7}],
			[{num:4},{num:0},{num:7}],
			[{num:4},{num:0},{num:8}],
			[{num:4},{num:9},{num:7}],
			[{num:7},{num:4},{num:9}],
		],

		prize:0,

		page:1,
		loadEnd:false,
		
		isShowAdd:false,
		selList:[]
    },
    async onLoad (options) {
        this.data.openID = await getOpenId()

        await this.getPeriodWinData();

		this.loadmore();
    },
	loadmore(){
		if( this.data.loadEnd ) return;
		let data = this.setPaginate(this.data.defaultList,10,this.data.page);
		if( !data||!data.length ){
			this.data.loadEnd = true;
			this.setData({ loadEnd:this.data.loadEnd })
			return
		}

		let list = this.data.list;
		if( this.data.page==1 ) list = [];
		list = list.concat(data);
		this.data.page++;
		this.data.list = list;
		this.setData({ list:this.data.list })
	},
	// 设计分页
	setPaginate(list,size,page){
		let start = ( page-1 )*size;
		let end = page*size;
		return list.slice(start,end)
	},
	close(){
		this.setData({ isShowAdd:false })
	},
	// 添加一直
	add(e){
		this.setData({
			isShowAdd:true,
			selList:[]
		})
	},
	submitClick(){
		let selList = this.data.selList.map(item=>{
			return { num:item }
		});
		let defaultList = this.data.defaultList;
		let code = this.data.code + 1;
		if( defaultList.some(e=>e.code==code) ){
			defaultList = defaultList.map(item=>{
				item.code = Number(item.code);
				let my = item.my;
				my = [].concat(my,[selList])
				item.my = my;
				return item;
			})
		}else{
			defaultList.unshift({
				code: code,
				isBuy:true,
				my:[selList],
				red:""
			})
		}

		writeFile(defaultList,"3d")

		this.setData({
			isShowAdd:false,
			defaultList:defaultList
		})
		this.data.page = 1;
		this.data.loadEnd = false;

		this.loadmore();
	},
	selectClick(e){
		let { item } = e.currentTarget.dataset;
		this.data.selList.push(item)
		this.setData({
			selList:this.data.selList,
		})
	},
	// 标记为购买
	isBuyClick(e){
		let { index } = e.currentTarget.dataset;
		let { list,defaultList } = this.data;

		if( list[index].isBuy ){
			wx.showToast({ title:"已标记",icon:"none" })
			return
		}

		wx.showModal({
			content:"是否确认购买本期？",
			success:(res)=>{
				if (res.confirm) {
					list[index].isBuy = true;
					list[index].my = common.depthCopy(this.data.my).map(e=>{
						let red = list[index].red.split(",")
						if( red[0]==e[0].num ) e[0].win = true;
						if( red[1]==e[1].num ) e[1].win = true;
						if( red[2]==e[2].num ) e[2].win = true;
						return e;
					})
					defaultList = defaultList.map(item=>{
						if( item.code==list[index].code ){
							item.isBuy = list[index].isBuy;
							item.my = list[index].my;
						}
						return item;
					})
					this.setData({ list:this.data.list,defaultList:this.data.defaultList });
				
					writeFile(defaultList,"3d");
					wx.showToast({ title:"标记为购买",icon:"none" })
				}
			}
		})
	},
	async getPeriodWinData(){
		return new Promise(async (resolve,reject)=>{
			let defaultList = await request("3d");

			let { code,data,red,win } = await this.getDay();
			this.setData({ code:Number(code),data,win })
			// 是否需要更新
			if( !defaultList.some(item=>item.code==code) ){
				defaultList.unshift({ code,data,red });
				writeFile(defaultList,"3d")
			}else{
				let isChange = false;
				defaultList = defaultList.map(item=>{
					if( item.code==code&&!item.red&&red ){
						item.red = red;
						isChange = true;
					}
					return item;
				})
				writeFile(defaultList,"3d")
			}
			this.data.defaultList = defaultList;
			this.data.prize = defaultList.filter(item=>item.isBuy).length*10

			this.setData({ defaultList:this.data.defaultList,prize:this.data.prize });
			resolve();
		})
	},
	getDay(){
		return new Promise((resolve,reject)=>{
			wx.request({
				url: 'https://caipiao.ip138.com/3d/',
				dataType:"其他",
				responseType:"text",
				enableHttp2:true,
				complete:(res=>{
					let html = res.data.replace(' ','');
					html = html.replace('    ','');
					html = html.replace('                               ','');
					html = html.replace('\n\n','');
					html = html.replace('\n\n','');
					let star = html.indexOf('<span class="period">');
					var period = html.substring(star+21,star+21+7);
					let redball = 'cache.ip138.com/public/image/award/redball-';
					var i1 = html.indexOf(redball);
					var str = html.substr(i1-12,520)
					str = str.replace(/\/\/cache.ip138.com\/public\/image\/award\/redball-/g,'');
					str = str.replace(/\/\/cache.ip138.com\/public\/image\/award\/blueball-/g,'');
					str = str.replace(/\n/g,'');

					var b=/alt="\d*"/g
					var s=str.match(b)
					let win = []
					for(var i= 0;i<s.length;i++){
						let num = s[i].replace("alt=",'');
						num = num.replace(/"/g,'');
						win.push(num);
					}
					resolve({
						date:"",
						win:win,
						red:win.join(","),
						code:period
					})
				})
			})
		})
	}
})